﻿using NHibernate.Collection.Generic;
using System;
using System.Collections;
using System.Collections.Generic;

namespace Inovout.Models
{
    public class DataObjectSet<TModel> : HashSet<TModel>, IDataObjectSet<TModel>
        , ISet<TModel>
    {
        public DataObjectSet() { }
        public DataObjectSet(ICollection<TModel> list)
        {
            this.AddAll(list);
        }

        private Action<TModel> setOwner;
        public DataObjectSet(Action<TModel> setOwner)
        {
            this.setOwner = setOwner;
        }

        public new virtual bool Add(TModel item)
        {
            if (base.Add(item))
            {
                if (setOwner != null)
                {
                    setOwner(item);
                }
                return true;
            }
            return false;
        }
        public bool AddAll(ICollection<TModel> collection)
        {
            bool changed = false;
            foreach (TModel o in collection)
            {
                changed |= this.Add(o);
            }
            return changed;
        }
        public bool ContainsAll(ICollection<TModel> collection)
        {
            foreach (TModel o in collection)
            {
                if (!this.Contains(o))
                {
                    return false;
                }
            }
            return true;
        }
     }
}